package net.zoneland.knowledge.dao;

import net.zoneland.knowledge.controller.bean.TermInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 文档校验-名词信息管理mapper.
 */
@Mapper
public interface TermInfoMapper {

    /**
     * 名词信息查询.
     * @param termInfo 名词对象.
     * @return 返回名词列表.
     */
    @Select({
        "<script>",
            "select t.id, t.term_name, t.term_type, t.substitute_word, t.examples",
            "from AITK_DOCUMENT_TERMS t ",
            "WHERE  1 = 1 ",
            "<if test=\"termName != null and termName != ''\"> AND t.term_name like '%' || #{termName,jdbcType = VARCHAR} || '%' </if> ",
            "<if test=\"termType != null and termType != ''\"> AND t.term_type = #{termType,jdbcType = VARCHAR}</if> ",
            "<if test=\"substituteWord != null and substituteWord != ''\"> AND t.substitute_word like '%' ||  #{substituteWord,jdbcType = VARCHAR} || '%' </if> ",
            "<if test=\"examples != null and examples != ''\"> AND t.examples like '%' ||  #{examples,jdbcType = VARCHAR} || '%' </if> ",
            "order by update_time desc ",
        "</script>"
    })
    @Results(id = "termInfo", value = {
        @Result(column="ID", property="id", jdbcType= JdbcType.VARCHAR, id=true),
        @Result(column="term_name", property="termName", jdbcType=JdbcType.VARCHAR),
        @Result(column="term_type", property="termType", jdbcType=JdbcType.VARCHAR),
        @Result(column="substitute_word", property="substituteWord", jdbcType=JdbcType.VARCHAR),
        @Result(column="examples", property="examples", jdbcType=JdbcType.VARCHAR)
    })
    List<TermInfo> search(final TermInfo termInfo);


    /**
     * 通过名词查询名词信息是否存在.
     * @param termName 名词.
     * @return 返回是否存在 true false.
     */
    @Select({
            "SELECT ",
            "CASE ",
            "WHEN COUNT(1) > 0 THEN 1 ",
            "ELSE 0 ",
            "END AS result ",
            "FROM AITK_DOCUMENT_TERMS ",
            "WHERE term_name = #{termName,jdbcType = VARCHAR}",
    })
    boolean isTermExist(@Param("termName") final String termName);

    /**
     * 通过id主键删除词库数据.
     * @param id 主键id.
     */
    @Delete({
            "delete from AITK_DOCUMENT_TERMS",
            "where ID = #{id,jdbcType=VARCHAR}"
    })
    void delete(final String id);

    /**
     * 通过id主键删除词库数据.
     * @param ids 主键id数组列表.
     */
    @Delete({
            "<script>",
            "delete from AITK_DOCUMENT_TERMS",
            "where ID in ",
            "<foreach collection=\"array\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">",
            "#{id,jdbcType=VARCHAR}",
            "</foreach>",
            "</script>"
    })
    void deleteBatch(final String[] ids);

    /**
     * 修改名词.
     * @param termInfo 名词信息.
     */
    @Update({
            "update AITK_DOCUMENT_TERMS",
            "set term_name = #{termName, jdbcType = VARCHAR}, ",
            "term_type = #{termType, jdbcType = VARCHAR}, ",
            "substitute_word = #{substituteWord, jdbcType = VARCHAR}, ",
            "examples = #{examples, jdbcType = VARCHAR} ",
            "WHERE ID = #{id, jdbcType = VARCHAR} "
    })
    void updateTerm(final TermInfo termInfo);

    /**
     * 新增名词词典信息.
     * @param termInfo 名词信息.
     */
    @Insert({
            "INSERT INTO AITK_DOCUMENT_TERMS (ID,term_name,term_type,substitute_word,examples) ",
            "values (#{id,jdbcType=VARCHAR}, #{termName,jdbcType=VARCHAR}, #{termType,jdbcType=VARCHAR}, ",
                    "#{substituteWord,jdbcType=VARCHAR}, #{examples,jdbcType=VARCHAR})"
    })
    void insert(final TermInfo termInfo);
}
